import redis


class MyRedis:
    def __init__(self, redis_host="localhost", redis_port=6379, redis_db=0, redis_password=None,
                 redis_prefix="fastapi"):
        self.r = redis.Redis(
            host=redis_host,
            port=int(redis_port),
            db=int(redis_db),
            password=redis_password,
        )
        self.prefix = redis_prefix

    def set(self, key, value: str):
        return self.r.set(f"{self.prefix}:{key}", value)

    def get(self, key):
        return self.r.get(f"{self.prefix}:{key}")

    def close(self):
        self.r.close()

    def set_rtd(self, key, value: str):
        return self.set(f"rtd:{key}", value)

    def get_rtd(self, key):
        return self.get(f"rtd:{key}")

    def set_token(self, key, value: str):
        return self.set(f"token:{key}", value)

    def get_token(self, key):
        return self.get(f"token:{key}")

    def get_custom(self, key):
        return self.get(f"custom:{key}")

    def set_custom(self, key, value: str):
        return self.set(f"custom:{key}", value)

    def __del__(self):
        self.close()
